﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.SiteAdmin;
using Jzy.Model.SiteAdmin;
using Jzy.Core.Common.Web;
using Jzy.Core.Attach;

using Jzy.Core.Utility;
using System.IO;
using Jzy.Common;

namespace Jzy.Web.SiteAdmin.Picture
{
    public partial class PictureEdit : PageEdit
    {
        private ArticleBO _article;
        private ArticleBO Article
        {
            get
            {
                if (_article == null)
                {
                    _article = new ArticleBO();
                }
                return _article;
            }
        }

        private string MenuID
        {
            get
            {
                return Request.QueryString["MenuID"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.PKID == null)
                {
                    Orders.Text = Article.GetMaxOrder().ToString();
                }
                else
                {
                    DispInfo();
                }
            }
            AddAttr();
        }

        private void AddAttr()
        {
            // btnSave.Attributes.Add("onclick", "pic_upload(this,'" + lblMessage.ClientID + "','" + pnlMain.ClientID + "');");
        }

        private void DispInfo()
        {
            ArticleInfo info = this.Article.GetInfo(this.PKID);
            EntityUtil.SetControl(pnlMain, info);

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ArticleInfo info = Article.GetInfo(PKID);
                EntityUtil.SetEntity(pnlMain, info);


                info.MenuID = int.Parse(MenuID);
                info.MenuName = new MenuBO().GetName(MenuID);

                if (string.IsNullOrEmpty(PKID))
                {
                    info.InputTime = DateTime.Now;
                }
                info.UpdateTime = DateTime.Now;
                info.CreatorID = AppSession.UserID;
                info.CreatorName = AppSession.UserName;
                info.IsDisplay = true;
                info.Source = Source.Text.Trim();

                if (Name.Text.Trim() == "")
                {
                    info.Name = Path.GetFileNameWithoutExtension(fileTitlePicture.FileName);
                }

                if (fileTitlePicture.FileName != "")
                {
                    if (!string.IsNullOrEmpty(info.SmallImage))
                    {
                        _article.DeleteTitlePicture(info.ID);
                    }

                    this.Article.SaveImage(info, fileTitlePicture.PostedFile);                   
                }

                this.Article.Save(info);
                JSUtil.RefreshView();
            }
            catch (Exception ex)
            {
                Label lbl = new Label();
                lbl.Text = ex.Message;
                pnlMain.Controls.Clear();
                pnlMain.Controls.Add(lbl);
            }
        }
    }
}
